package com.heima.app.gateway.filter;

import cn.hutool.core.util.StrUtil;
import com.heima.app.gateway.config.MyConfig;
import com.heima.utils.common.AppJwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {

    @Resource
    private MyConfig myConfig;
    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1. 判断请求是否在白名单中，如果是就放行
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String[] noAuthPaths = this.myConfig.getNoAuthPaths();
        for (String noAuthPath : noAuthPaths) {
            String uri = request.getURI().getPath();
            if (antPathMatcher.match(noAuthPath, uri)) {
                //放行
                return chain.filter(exchange);
            }
        }

        //2. 校验是否有token，如果没有响应401
        String token = request.getHeaders().getFirst("token");
        if (StrUtil.isEmpty(token)) {
            //拦截请求
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        //3. 校验token的有效性能
        try {
            Claims claimsBody = AppJwtUtil.getClaimsBody(token);
            int code = AppJwtUtil.verifyToken(claimsBody);
            if (code == 1 || code == 2) {
                //token过期
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
        } catch (Exception e) {
            //拦截请求
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        //4. token有效的，放行
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }

    public static void main(String[] args) {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        System.out.println(antPathMatcher.match("/**/doc.html", "/user/doc.html"));
        System.out.println(antPathMatcher.match("/**/doc.html", "/api/aa/bb/user/doc.html"));
        System.out.println(antPathMatcher.match("/**/doc.html", "/doc.html"));
        System.out.println(antPathMatcher.match("/**/doc.html/**", "/user/doc.html/abc"));
        System.out.println(antPathMatcher.match("/**/doc.html", "/user/doc.html#/default/ap-user-login-controller/loginUsingPOST"));
        System.out.println(antPathMatcher.match("/**/login/**", "/user/v1/api/login/query"));
    }
}
